package com.dshore.messagecenter.service.warning.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dshore.messagecenter.dao.warning.WarningThresholdDao;
import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.model.system.SysDict;
import com.dshore.messagecenter.model.warning.MrWarningPerson;
import com.dshore.messagecenter.model.warning.MrWarningThreshold;
import com.dshore.messagecenter.model.warning.WarningShow;
import com.dshore.messagecenter.service.warning.WarningThresholdService;
import com.dshore.messagecenter.util.Page;
import com.dshore.messagecenter.util.UUIDUtil;
@Service
public class WarningThresholdServiceImpl implements WarningThresholdService{
	
	@Autowired
	private WarningThresholdDao warningThresholdDao;
	/**
	 * 获取监控项下拉框数值
	 */
	@Override
	public List<SysDict> getMonitoringItems(String objType) {
		return warningThresholdDao.getMonitoringItems(objType);
	}
	/**
	 * 获取监控时间段
	 */
	@Override
	public List<SysDict> getMonitoringPeriod() {
		return warningThresholdDao.getMonitoringPeriod();
	}
	/**
	 * 监控阈值设置
	 */
	@Override
	public List<SysDict> getThreshold(String objType,String kpiType) {
		return warningThresholdDao.getThreshold(objType,kpiType);
	}
	/**
	 * 预警通知人
	 */
	@Override
	public List<MrWarningPerson> getPrincipal() {
		return warningThresholdDao.getPrincipal();
	}
	
	/**
	 * 通知方式
	 */
	@Override
	public List<SysDict> getWarningType() {
		return warningThresholdDao.getWarningType();
	}
	
	/**
	 * 删除
	 */
	@Override
	public ResponseResult<Void> deleteByIds(String ids) {
		ResponseResult<Void> responseResult = new ResponseResult<Void>(200,"删除成功");
		String[] idArr = ids.split(",");
		warningThresholdDao.deleteByIds(idArr);
		return responseResult;
	}
	
	/**
	 * 页面表格数据展示
	 */
	@Override
	public Page<MrWarningThreshold> queryListAll(MrWarningThreshold warningThreshold) {
		// 总记录数
		long total = warningThresholdDao.queryCountByPrama(warningThreshold);
		// 模糊查询
		List<MrWarningThreshold> list = warningThresholdDao.queryListByPrama(warningThreshold);
		// 重构对象,方便扩展
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
			for (MrWarningThreshold warningThreshold1 : list) {	
				WarningShow thresholdTable = warningThresholdDao.selectThresholdTable(warningThreshold1);			
					if(thresholdTable==null) {
						total-=1;
						continue;
					}else {
						Map<String, Object> map = new HashMap();
						map.put("kpiName",thresholdTable.getKpiName());
						map.put("objtype",thresholdTable.getObjtype());
						map.put("obj", thresholdTable.getObj());
						map.put("periodTime", thresholdTable.getPeriodTime());
						String threshold = thresholdTable.getSymbol()+warningThreshold1.getMonitoringThreshold()+thresholdTable.getUnitName();
						map.put("threshold", threshold);
						map.put("principal", thresholdTable.getPeople());
						map.put("informWay", thresholdTable.getInformWay());
						mapList.add(map);
					}
			}
		//Page<MrWarningThreshold> page = new Page<MrWarningThreshold>(warningThreshold.getLimit(), total, warningThreshold.getOffset(),mapList);		
		//return page;
			return null;
		
	}
	
	/**
	 * 获取监控类型的下拉框值
	 */
	@Override
	public List<SysDict> getMonitoringType() {
		 return warningThresholdDao.getMonitoringType();
	}
	
	/**
	 * 修改查询单个数据
	 */
	@Override
	public MrWarningThreshold getById(String id) {
		return warningThresholdDao.getById(id);
	}
	
	/**
	 * 新增
	 */
	@Override
	public ResponseResult<Void> addWarningThreshold(MrWarningThreshold warningThreshold) {
		ResponseResult<Void> responseResult = new ResponseResult<Void>(200,"新增成功");
		warningThreshold.setId(UUIDUtil.getUUId());
		Integer i=warningThresholdDao.addWarningThreshold(warningThreshold);
		if(i<0) {
			responseResult.setState(400);
			responseResult.setMessage("新增失败");
		}
		return responseResult;
	}
	
	/**
	 * 修改
	 */
	@Override
	public ResponseResult<Void> changeWarningThreshold(MrWarningThreshold warningThreshold) {
		ResponseResult<Void> responseResult = new ResponseResult<Void>(200,"修改成功");
		Integer i= warningThresholdDao.changeWarningThreshold(warningThreshold);
		if(i<0) {
			responseResult.setState(400);
			responseResult.setMessage("修改失败");
		}
		return responseResult;
	}
	
	/**
	 * 获取单位
	 */
	@Override
	public List<SysDict> getUnit(String objType,String kpiType) {		
		return warningThresholdDao.getUnit(objType,kpiType);
	}
	
	
}
